package com.kgc.dao.impl;

import com.kgc.dao.BaseDao;
import com.kgc.dao.NewsDao;
import com.kgc.entity.News;
import com.kgc.util.PageSupport;

import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <h3>项目名称:demo03050</h3>
 * <p>本API描述:新闻数据操作实现类</p>
 * <p>@author : AliyZhang</p>
 * <p>@date : 2021-03-05 19:09</p>
 **/
public class NewsDaoImpl extends BaseDao implements NewsDao {
    @Override
    public int addNews(News news) {
        //SQL语句
        String sql="insert into news_detail(categoryId,title,summary,content,picPath,author,createDate) " +
                "values(?,?,?,?,?,?,?)";
        //参数数组
        Object[] params=new Object[7];
        params[0]=news.getCategoryId();
        params[1]=news.getTitle();
        params[2]=news.getSummary();
        params[3]=news.getContent();
        params[4]=news.getPicPath();
        params[5]=news.getAuthor();
        params[6]=news.getCreateDate();
        //调用从父类baseDao继承的增删改方法
        int rows=executeUpdate(sql,params);
        return rows;
    }

    @Override
    public int deleteNewsById(int id) {
        //SQL语句
        String sql="delete from news_detail where id=?";
        //参数数组
        Object[] params=new Object[1];
        params[0]=id;
        return executeUpdate(sql,params);
    }

    @Override
    public int updateNews(News news) {
        //SQL语句
        String sql="update news_detail set categoryId=?,title=?," +
                "summary=?,content=?,picPath=?," +
                "author=?,modifyDate=? where id=?";
        Object[] params=new Object[8];
        params[0]=news.getCategoryId();
        params[1]=news.getTitle();
        params[2]=news.getSummary();
        params[3]=news.getContent();
        params[4]=news.getPicPath();
        params[5]=news.getAuthor();
        params[6]=news.getModifyDate();
        params[7]=news.getId();
        return executeUpdate(sql,params);
    }

    @Override
    public List<News> queryNewsAll() {
        //SQL语句
        String sql="select * from news_detail";
        ResultSet rs = executeQuery(sql, null);
        //创建list集合，封装数据
        List<News> list=new ArrayList<>();
        try {
            while (rs.next()){
            int id=rs.getInt("id");
            int categoryId=rs.getInt("categoryId");
            String title=rs.getString("title");
            String summary=rs.getString("summary");
            String content=rs.getString("content");
            String picPath=rs.getString("picPath");
            String author=rs.getString("author");
            Date createDate=rs.getDate("createDate");
            Date modifyDate=rs.getDate("modifyDate");
            //把集合中的数据转化为一个新闻对象
            News news = new News(id, categoryId, title, summary, content, picPath, author, createDate, modifyDate);
            //将对象存入list集合中
            list.add(news);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public News queryNewsById(int id) {
        //获取SQL字符串
        String sql="select * from news_detail where id=?";
        //参数数组
        Object[] params=new Object[1];
        params[0]=id;
        //调用通用的查询方法
        ResultSet rs = executeQuery(sql, params);
        News news=null;
        try {
            while (rs.next()) {
                int categoryId = rs.getInt("categoryId");
                String title = rs.getString("title");
                String summary = rs.getString("summary");
                String content = rs.getString("content");
                String picPath = rs.getString("picPath");
                String author = rs.getString("author");
                Date createDate = rs.getDate("createDate");
                Date modifyDate = rs.getDate("modifyDate");
                //把集合中的数据转化为一个新闻对象
                news = new News(id, categoryId, title, summary, content, picPath, author, createDate, modifyDate);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return news;
    }

    @Override
    public int queryNewsTotal() {
        //SQL语句
        String sql="select count(id) from news_detail";
        //调用通用的查询方法
        ResultSet rs = executeQuery(sql, null);
        int total=0;
        try {
            if(rs.next()){
                total=rs.getInt(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return total;
    }

    @Override
    public void queryPageNews(PageSupport<News> page) {
        /*
        page对象刚传过来时有：
        ①当前页码；②每页数据容量；③每页数据集合；④数据总条数信息；⑤总页数
         ?,?
         分页查询的起始位置（当前页码-1）*pageSize
         每页的数据条数
         */
        //SQL语句
        String sql="select * from news_detail order by id limit ?,?";
        //参数数组
        Object[] params=new Object[2];
        params[0]=(page.getCurPageNo()-1)*page.getPageSize();
        params[1]=page.getPageSize();
        //调用通用的查询方法
        ResultSet rs = executeQuery(sql, params);
        //声明集合，存储查询出来的数据
        List<News> list=new ArrayList<>();
        try {
            while (rs.next()) {
                int id = rs.getInt("id");
                int categoryId = rs.getInt("categoryId");
                String title = rs.getString("title");
                String summary = rs.getString("summary");
                String content = rs.getString("content");
                String picPath = rs.getString("picPath");
                String author = rs.getString("author");
                Date createDate = rs.getDate("createDate");
                Date modifyDate = rs.getDate("modifyDate");
                //把集合中的数据转化为一个新闻对象
                News news = new News(id, categoryId, title, summary, content, picPath, author, createDate, modifyDate);
                //将对象存入list集合中
                list.add(news);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //把查询出来的本页数据存储到page对象里
        page.setDatas(list);
        //在page里设置总条数信息
        page.setTotal(queryNewsTotal());
    }

    public static void main(String[] args) {
        NewsDaoImpl newsDao = new NewsDaoImpl();
        List<News> list = newsDao.queryNewsAll();
        System.out.println("list = " + list);
    }

}
